package com.example.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Constants;
import com.example.mapper.ConfigMapper;
import com.example.pojo.Config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.Optional;

/** 配置表参数查询初始化
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/1/25
 * @time: 19:13
 */
@Slf4j
@Configuration
public class ConfigTableValueConfig {
    @Resource
    private ConfigMapper configMapper;
    // 系统储存根路径
    private String resRootPath;
    // 头像存储路径
    private String avatarStorePath;
    // 头像链接路径
    private String avatarNetUrl;
    // 单位图片存储路径
    private String entPhotosStorePath;
    // 单位图片链接路径
    private String entPhotosNetUrl;
    // 文章封面存储路径
    private String essayCoversStorePath;
    // 文章封面链接路径
    private String essayCoversNetUrl;
    // 文章内容存储路径
    private String essayTxtStorePath;
    // 文章内容链接路径
    private String essayTxtNetUrl;
    // 文章图片存储路径
    private String essayPicsStorePath;
    // 文章图片链接路径
    private String essayPicsNetUrl;
    // 医生签名图片存储路径
    private String drSignStorePath;
    // 医生签名图片链接路径
    private String drSignNetUrl;


    @PostConstruct
    public void init() {
        // 初始化逻辑
        LambdaQueryWrapper<Config> lqw = new LambdaQueryWrapper<>();
        if (SystemUtils.IS_OS_WINDOWS) {
            // 查询系统储存根路径
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_RES_PATH_CODE);
            resRootPath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询头像存储路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_AVATAR_PATH_CODE);
            avatarStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询单位图片储存路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_ENT_PHOTOS_PATH_CODE);
            entPhotosStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询文章封面存储路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_ESSAY_COVERS_PATH_CODE);
            essayCoversStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询文章内容存储路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_ESSAY_TXT_PATH_CODE);
            essayTxtStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询文章图片存储路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_ESSAY_PICS_PATH_CODE);
            essayPicsStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
            // 查询医生签名图片存储路径
            lqw.clear();
            lqw.eq(Config::getCode, Constants.CONFIG_WINDOWS_DR_SIGN_PATH_CODE);
            drSignStorePath = Optional.of(configMapper.selectOne(lqw)).orElse(new Config()).getValue();
        } else if (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) {
            // Linux或Mac操作系统

        } else {
            // 其他操作系统

        }
        // 检查配置项是否完备
        if (!StringUtils.hasText(resRootPath) || !StringUtils.hasText(avatarStorePath) ||
                !StringUtils.hasText(entPhotosStorePath) || !StringUtils.hasText(essayCoversStorePath) ||
                !StringUtils.hasText(essayTxtStorePath) || !StringUtils.hasText(essayPicsStorePath)) {
            log.error("系统配置异常");
            System.exit(0);
        }
        // 转换路径拼接符，获得头像链接路径
        avatarNetUrl = Constants.NET_RES_PATH_PREFIX + avatarStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终头像存储路径
        avatarStorePath = resRootPath + avatarStorePath;

        // 转换路径拼接符，获得单位图片链接路径
        entPhotosNetUrl = Constants.NET_RES_PATH_PREFIX + entPhotosStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终单位图片存储路径
        entPhotosStorePath = resRootPath + entPhotosStorePath;

        // 转换路径拼接符，获得文章封面链接路径
        essayCoversNetUrl = Constants.NET_RES_PATH_PREFIX + essayCoversStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终文章封面存储路径
        essayCoversStorePath = resRootPath + essayCoversStorePath;

        // 转换路径拼接符，获得文章内容链接路径
        essayTxtNetUrl = Constants.NET_RES_PATH_PREFIX + essayTxtStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终文章内容存储路径
        essayTxtStorePath = resRootPath + essayTxtStorePath;

        // 转换路径拼接符，获得文章内容链接路径
        essayPicsNetUrl = Constants.NET_RES_PATH_PREFIX + essayPicsStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终文章内容存储路径
        essayPicsStorePath = resRootPath + essayPicsStorePath;

        // 转换路径拼接符，获得医生签名链接路径
        drSignNetUrl = Constants.NET_RES_PATH_PREFIX + drSignStorePath.replace(File.separator, Constants.FORWARD_SLASH);
        // 拼接系统储存根路径，获得最终医生签名存储路径
        drSignStorePath = resRootPath + drSignStorePath;
    }

    public String getResRootPath() {
        return resRootPath;
    }

    public String getAvatarStorePath() {
        return avatarStorePath;
    }

    public String getAvatarNetUrl() {
        return avatarNetUrl;
    }

    public String getEntPhotosStorePath() {
        return entPhotosStorePath;
    }

    public String getEntPhotosNetUrl() {
        return entPhotosNetUrl;
    }

    public String getEssayCoversStorePath() {
        return essayCoversStorePath;
    }

    public String getEssayCoversNetUrl() {
        return essayCoversNetUrl;
    }

    public String getEssayTxtStorePath() {
        return essayTxtStorePath;
    }

    public String getEssayTxtNetUrl() {
        return essayTxtNetUrl;
    }

    public String getEssayPicsStorePath() {
        return essayPicsStorePath;
    }

    public String getEssayPicsNetUrl() {
        return essayPicsNetUrl;
    }

    public String getDrSignStorePath() {
        return drSignStorePath;
    }

    public String getDrSignNetUrl() {
        return drSignNetUrl;
    }
}
